import java.util.Scanner;

public class PasswordManager {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int choice;//循环
        do {
            System.out.println("密码管理系统菜单:");
            System.out.println("1. 加密密码");
            System.out.println("2. 解密密码");
            System.out.println("3. 判断密码强度");
            System.out.println("4. 生成密码");
            System.out.println("5. 退出");
            System.out.print("请选择操作（1-5）：");

            choice = input.nextInt();

            switch (choice) {
                case 1:
                    System.out.print("请输入要加密的密码：");
                    input.nextLine(); // 消耗换行符
                    String passwordToEncrypt = input.nextLine();
                    String strongerPasswords = strongerPasswords(passwordToEncrypt);
                    System.out.println("加密后的密码：" + strongerPasswords);
                    break;
                case 2:
                    System.out.print("请输入要解密的密码：");
                    input.nextLine(); // 消耗换行符
                    String passwordToDecrypt = input.nextLine();
                    String lowerPasswords = lowerPasswords(passwordToDecrypt);
                    System.out.println("解密后的密码：" + lowerPasswords);
                    break;
                case 3:
                    System.out.print("请输入要判断强度的密码：");
                    input.nextLine(); // 消耗换行符
                    String passwordToCheck = input.nextLine();
                    String strength = checkPasswordStrength(passwordToCheck);
                    System.out.println("密码强度：" + strength);
                    break;
                case 4:
                    System.out.print("输入要生成的密码长度：");
                    int passwordLength = input.nextInt();
                    String generatedPassword = generatePassword(passwordLength);
                    System.out.println("生成的密码：" + generatedPassword);
                    break;
                case 5:
                    System.out.println("感谢使用密码管理系统，再见！");
                    break;
                default:
                    System.out.println("无效的选项，请重新选择！");
                    break;
            }
        } while (choice != 5);
    }

    // 加密密码
    public static String  strongerPasswords(String password){        //加密
        StringBuilder strongerPasswords=new StringBuilder();                            //创建StringBulider对象，StringBuilder是Java中的一个类，它表示可变的字符序列，可方便修改重构字符串
        for(int i=0;i<password.length();i++){
            char A=password.charAt(i);                                                  //遍历每一个字符
            char charStronger=(char)(A+ (1+i)+3);                                        //处理strongerPasswords对象中每一个字符（下标+1，ascii码值加3）
            strongerPasswords.append(charStronger);                                     //将得到的新的字符追加到当前字符的末尾
        }
        String firstChar=String.valueOf(strongerPasswords.charAt(0));              //将字符串strongerPasswords的第一个字符转换为字符串类型，并将结果赋给变量firstChar。
        String lastChar=String.valueOf(strongerPasswords.charAt(strongerPasswords.length()-1)); //将字符串strongerPasswords最后一个字符转换为字符串，并将结果赋给变量lastChar。
        String middleString=strongerPasswords.substring(1,strongerPasswords.length()-1);    //这段字符的第一个和最后一个字符除去，只保留中间部分，并赋值给变量middleString
        strongerPasswords=new StringBuilder(lastChar+middleString+firstChar);      //创建一个新的StringBulider对象，将上述三个变量按lastChar->middleString->firstChar的顺序组成该对象的字符串值
        strongerPasswords=strongerPasswords.reverse();                             //用reverse（）方法反转strongPasswords的字符
        //从而加密成功
        System.out.println("加密成功：   "+strongerPasswords);
        return strongerPasswords.toString();                                       //将strongerPasswords对象转换为字符串
    }

    // 解密密码
    public  static String lowerPasswords(String password){       //解密
        StringBuilder lowerPasswords=new StringBuilder(password);                //步骤与加密大体相同，部分相反
        lowerPasswords=lowerPasswords.reverse();
        String middleString=lowerPasswords.substring(1,lowerPasswords.length()-1);
        String lastChar=String.valueOf(lowerPasswords.charAt(lowerPasswords.length()-1));
        String firstChar=String.valueOf(lowerPasswords.charAt(0));
        lowerPasswords =new StringBuilder(lastChar+middleString+firstChar);
        System.out.println(lowerPasswords);
        StringBuilder lower=new StringBuilder();
        for(int i=0;i<lowerPasswords.length();i++){
            char B=lowerPasswords.charAt(i);
            char charlow=(char)((B-i-1-3));
            lower.append(charlow);
        }
        System.out.println("解密成功:   "+lower);
        return lower.toString();
    }

    // 判断密码强度
    public static String checkPasswordStrength(String password) {
        int length = password.length();
        boolean hasDigit = false;//数字
        boolean hasLowercase = false;//小写字母
        boolean hasUppercase = false;//大写字母

        for (char c : password.toCharArray()) {//方法判断其是否为数字、小写字母和大写字母，并相应地更新对应的布尔变量。
            if (Character.isDigit(c)) {
                hasDigit = true;
            } else if (Character.isLowerCase(c)) {
                hasLowercase = true;
            } else if (Character.isUpperCase(c)) {
                hasUppercase = true;
            }
        }

        if (length < 8 || (!hasDigit && !hasLowercase && !hasUppercase)) {
            return "弱强度";
        } else if (hasDigit && (hasLowercase || hasUppercase)) {
            return "高强度";
        } else {
            return "中强度";
        }
    }

    // 生成密码
    public static String generatePassword(int length) {
        String uppercaseChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        String lowercaseChars = "abcdefghijklmnopqrstuvwxyz";
        String digitChars = "0123456789";

        StringBuilder password = new StringBuilder();

        for (int i = 0; i < length; i++) {
            int type = (int) (Math.random() * 3); // 0: 大写字母, 1: 小写字母, 2: 数字

            switch (type) {
                case 0:
                    password.append(uppercaseChars.charAt((int) (Math.random() * 26)));
                    break;
                case 1:
                    password.append(lowercaseChars.charAt((int) (Math.random() * 26)));
                    break;
                case 2:
                    password.append(digitChars.charAt((int) (Math.random() * 10)));
                    break;
            }
        }

        return password.toString();
    }

    // 交换字符串的第一个字符和最后一个字符
    public static String swapFirstAndLastChar(String str) {
        if (str.length() <= 1) {
            return str;
        }

        char firstChar = str.charAt(0);
        char lastChar = str.charAt(str.length() - 1);

        StringBuilder result = new StringBuilder(str);
        result.setCharAt(0, lastChar);
        result.setCharAt(str.length() - 1, firstChar);

        return result.toString();
    }
}
